package org.shangrila.app.wfm.mb;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import java.util.Scanner;

import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;

import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.diagram.DefaultDiagramModel;
import org.shangrila.app.sys.shiro.SecUtil;
import org.shangrila.comm.helper.StringHelper;
import org.shangrila.wfm.core.WfmEngine;
import org.shangrila.wfm.core.WfmHelper;
import org.shangrila.wfm.eo.WfmProcess;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Named
@SessionScoped
public class WfmProcessMgrMB implements Serializable{
	private static final Logger log = LoggerFactory.getLogger(WfmProcessMgrMB.class);
	private static final long serialVersionUID = 4899456092581896951L;
	private String operator;  
	private String flowDisplayName="";  
	private List<?> voList=null ; 
	private int count = 10; 
	private WfmEngine engine = null;	

	private WfmProcess selectedProcess;

	private DefaultDiagramModel model;
	
	public WfmProcessMgrMB() {
		try{
			operator = SecUtil.getCachedShiroUser().getUserno();
		}
		catch(Exception ex){
			operator="wfm";
		}
		engine = WfmHelper.getEngine();
		queryFlowList();
	}

    //查询流程定义列表：参数为流程显示名称
	public String queryFlowList() {			
		if(StringHelper.isEmpty(flowDisplayName))
			voList=engine.getWfmdao().findProcessAll();
		else
			voList=engine.getWfmdao().findProcessByDisplayName(flowDisplayName);
		
		return null;
	}
	//deploy process
    public void handleFileUpload(FileUploadEvent event) {
		String filename = event.getFile().getFileName();
		try {

		    Scanner scanner = new Scanner(event.getFile().getInputstream(), "UTF-8");
		    String xmlText = scanner.useDelimiter("\\A").next();
		    scanner.close();

		    selectedProcess = engine.deployProcess(xmlText,operator);
			
			queryFlowList();
						
		} catch (IOException e) {
			showMessages("Failure", filename + " is uploaded and deployed failly.");
			log.error(filename + " is uploaded and deployed failly.");
			e.printStackTrace();
		}
		showMessages("成功发布流程", filename + " 流程成功发布.");
		log.info(filename + " is uploaded and deployed.");
    }

	//show process
    public String handleShowProcess() {
	    this.model = WfmDiagramModel.genProcessDiagramModel(selectedProcess,null);
    	return null;
    }

	//start process
    public String handleStartProcess() {
		engine.startService(selectedProcess.getId(), operator,null); //"operator"是流程实例创建者

		showMessages("Succesful", selectedProcess.getId() + " is started.");
    	return null;
    }

    //undeploy process
    public String handleUndeployProcess() {	
    	engine.undeployProcess(selectedProcess,operator);
    	showMessages("Succesful", selectedProcess.getId() + " is undeployed.");
    	return null;
    }
    //redeploy process
    public String handleRedeployProcess() {	
    	engine.redeployProcess(selectedProcess,operator);
    	showMessages("Succesful", selectedProcess.getId() + " is undeployed.");
    	return null;
    }
	
	//show faces Messages
	public void showMessages(String summary,String detail) {	
		FacesMessage message = new FacesMessage(summary, detail);
		FacesContext.getCurrentInstance().addMessage(null, message);
	}
	
	public String getOperator() {
		return operator;
	}

	public void setOperator(String operator) {
		this.operator = operator;
	}

	public String getFlowDisplayName() {
		return flowDisplayName;
	}

	public void setFlowDisplayName(String flowDisplayName) {
		this.flowDisplayName = flowDisplayName;
	}

	public List<?> getVoList() {
		return voList;
	}

	public void setVoList(List<?> voList) {
		this.voList = voList;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public WfmEngine getEngine() {
		return engine;
	}

	public void setEngine(WfmEngine engine) {
		this.engine = engine;
	}

	public WfmProcess getSelectedProcess() {
		return selectedProcess;
	}

	public void setSelectedProcess(WfmProcess selectedProcess) {
		this.selectedProcess = selectedProcess;
	}
	
	public DefaultDiagramModel getModel() {
		return model;
	}

	public void setModel(DefaultDiagramModel model) {
		this.model = model;
	}

}
